const MyBaseController = require('./MyBaseController');

// const {
// 	Controller
// } = require('egg');

class ArticleController extends MyBaseController {
  
    
    async selectById() {
        let { articleId } = this.ctx.params
        let res = await this.ctx.app.mysql.get('article', { id: articleId }
        )
        this.success(res)
    }


    async list() {
        const page = this.ctx.query.page | 1;
        const size = this.ctx.query.size | 5;
        let res = await this.ctx.app.mysql.select('article', {
            limit: size,
            offset: (page - 1) * size,
        });
        let record = {
            records: res
        }
        this.success(record)
    }

    async addArticle() {
        let article =  this.ctx.request.body;
        article.create_time=this.app.mysql.literals.now
        const result = await this.app.mysql.insert('article',article);
        if (result.affectedRows !== 1) {
            throw new Error('添加失败');
          }
        let image={
            url:article.phootoA,
            create_time:this.app.mysql.literals.now,
            is_valid:1
        }
        const result2= await this.app.mysql.insert('image',image);
        if (result2.affectedRows !== 1) {
            throw new Error('添加失败');
          }
        this.success(result)
    }

}

module.exports = ArticleController;